﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace CustomsSystem.DAL
{
    /// <summary>
    /// 数据访问类:Info_code
    /// </summary>
    public partial class CodeService
    {
        public CodeService()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("codeID", "Info_code");
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string codeType, int codeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Info_code");
            strSql.Append(" where codeType='" + codeType + "' and codeID=" + codeID + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(CustomsSystem.Model.Info_code model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.codeType != null)
            {
                strSql1.Append("codeType,");
                strSql2.Append("'" + model.codeType + "',");
            }
            if (model.codeID != null)
            {
                strSql1.Append("codeID,");
                strSql2.Append("" + model.codeID + ",");
            }
            if (model.codeName != null)
            {
                strSql1.Append("codeName,");
                strSql2.Append("'" + model.codeName + "',");
            }
            strSql.Append("insert into Info_code(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Info_code model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Info_code set ");
            if (model.codeName != null)
            {
                strSql.Append("codeName='" + model.codeName + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where codeType='" + model.codeType + "' and codeID=" + model.codeID + " ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string codeType, int codeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Info_code ");
            strSql.Append(" where codeType='" + codeType + "' and codeID=" + codeID + " ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Info_code GetModel(string codeType, int codeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" codeType,codeID,codeName ");
            strSql.Append(" from Info_code ");
            strSql.Append(" where codeType='" + codeType + "' and codeID=" + codeID + " ");
            CustomsSystem.Model.Info_code model = new CustomsSystem.Model.Info_code();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Info_code DataRowToModel(DataRow row)
        {
            CustomsSystem.Model.Info_code model = new CustomsSystem.Model.Info_code();
            if (row != null)
            {
                if (row["codeType"] != null)
                {
                    model.codeType = row["codeType"].ToString();
                }
                if (row["codeID"] != null && row["codeID"].ToString() != "")
                {
                    model.codeID = int.Parse(row["codeID"].ToString());
                }
                if (row["codeName"] != null)
                {
                    model.codeName = row["codeName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select codeType,codeID,codeName ");
            strSql.Append(" FROM Info_code ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" codeType,codeID,codeName ");
            strSql.Append(" FROM Info_code ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

